<?php
namespace Action;
class Admin extends Daniuwo{
    public function __construct(){
        parent::__construct();
        if(!IS_LOGIN){
            header('Location: '.DN_URLA('index','login'));
            exit;
        }
        $this->view = 'Admin';
    }
    // 首页
    public function index(){
        $this->v('title','仪表盘');
        // 盗版统计
        $daoban     = S('daoban')->count('*');
        $zhenban    = S('shouquan')->count('*');
        $guanbi    = S('shouquan')->count(['state'=>0]);

        // 最新6条授权
        $zhenban_list    = S('shouquan')->select('*',['ORDER' => ['atime'=>'DESC'],'LIMIT'=>5]);
        $daoban_list    = S('daoban')->select('*',['ORDER' => ['atime'=>'DESC'],'LIMIT'=>5]);
        // 最新6条盗版
        $this->v('daoban',$daoban);
        $this->v('zhenban',$zhenban);
        $this->v('zhenban_list',$zhenban_list);
        $this->v('guanbi',$guanbi);
        $this->v('daoban_list',$daoban_list);
        $this->display('index');
    }

    // 增加授权
    public function add()
    {
        $this->v('title','增加授权');
        if(IS_POST){
            $domain = htmlentities(X('post.domain'));
            $xiangmu= htmlentities(X('post.xiangmu'));
            $qq     = intval(X('post.qq'));
            $state  = intval(X('post.state'));
            if(empty($domain)){
                return $this->message('请输入域名');
            }
            if(empty($qq)){
                return $this->message('请输入QQ号');
            }
            if(S('shouquan')->count(['domain'=>$domain])){
                return $this->message('增加的域名已存在');
            }
            S('shouquan')->insert([
                'domain'=>$domain,
                'xiangmu'=>$xiangmu,
                'qq'    =>$qq,
                'state' =>$state,
                'atime' =>NOW_TIME
            ]);
            // 删除盗版记录
            S('daoban')->delete([
                'AND'=>[
                    'domain'=>$domain
                ]
            ]);
            return $this->message('增加成功',true);
        }
        $this->display('add');
    }
    // 编辑授权
    public function edit()
    {
        if(IS_POST){
            $id     = intval(X('post.id'));
            $domain = htmlentities(X('post.domain'));
            $xiangmu= htmlentities(X('post.xiangmu'));
            $qq     = intval(X('post.qq'));
            $state  = intval(X('post.state'));
            if(empty($domain)){
                return $this->message('请输入域名');
            }
            if(empty($qq)){
                return $this->message('请输入QQ号');
            }
            if(!S('shouquan')->count(['id'=>$id])){
                return $this->message('修改的内容不存在');
            }
            S('shouquan')->update([
                'domain'=>$domain,
                'xiangmu'=>$xiangmu,
                'qq'    =>$qq,
                'state' =>$state,
            ],['id'=>$id]);
            // 删除盗版记录
            S('daoban')->delete([
                'AND'=>[
                    'domain'=>$domain
                ]
            ]);
            return $this->message('修改成功',true);
        }
        return $this->message('请求错误');
    }
    // 修改密码
    public function edit_pass(){
        if(!IS_LOGIN)
            return $this->message('请登录');

        $gn = X('post.gn');
        if(IS_POST){
            $pass0 = X("post.pass0");
            $pass1 = X("post.pass1");
            $pass2 = X("post.pass2");
            if($pass1 != $pass2)
                return $this->message("两次密码不一致");
            $UserLib = L("User");
            if(!$UserLib->check_pass($pass1))
                return $this->message('密码不符合规则');
            if($UserLib->md5_md5($pass0,$this->_user['salt']) != $this->_user['pass'])
                return $this->message('原密码不正确');
            $newpass = $UserLib->md5_md5($pass1,$this->_user['salt']);
            $this->_user['pass'] = $newpass;
            S("User")->update(array(
                'pass'=>$this->_user['pass']
            ),[
                'uid'=>NOW_UID
            ]);
            cookie('HYBBS_HEX',$UserLib->set_cookie($this->_user));
            return $this->message("修改成功",true);
        }
        
        return $this->message("提交出错");
        

    }
    // 授权列表
    public function shouquan_list()
    {
        $this->v('title','授权列表');
        $pageid = intval(X('get.page')) ? intval(X('get.page')) : 1;
        $domain = x('get.domain');
        $qq     = X('get.qq');
        $shouquan = S("shouquan");
        if($domain){
            $shouquan_data = $shouquan->select('*',[
                'domain'=>$domain,
                'LIMIT' =>[($pageid-1) * 10, 10],
                'ORDER' => ['id'=>'DESC']
                ]);
        }elseif ($qq){
            $shouquan_data = $shouquan->select('*',[
                'qq'=>$qq,
                'LIMIT' =>[($pageid-1) * 10, 10],
                'ORDER' => ['id'=>'DESC']
                ]);
        }else{
            $shouquan_data = $shouquan->select('*',[
                'LIMIT' =>[($pageid-1) * 10, 10],
                'ORDER' => ['id'=>'DESC']
                ]);
        }
        $count = $shouquan->count('*');
        $count = (!$count)?1:$count;
        $page_count = ($count % 10 != 0)?(intval($count/10)+1) : intval($count/10);

        $this->v("pageid",$pageid);
        $this->v("page_count",$page_count);
        $this->v('data',$shouquan_data);
        $this->display('list');
    }
    // 盗版列表
    public function daoban_list()
    {
        $this->v('title','盗版列表');
        $pageid = intval(X('get.page')) ? intval(X('get.page')) : 1;
        
        $shouquan = S("daoban");
        
        $shouquan_data = $shouquan->select('*',[
            'LIMIT' =>[($pageid-1) * 10, 10],
            'ORDER' => ['id'=>'DESC']
            ]);
        $count = $shouquan->count('*');
        $count = (!$count)?1:$count;
        $page_count = ($count % 10 != 0)?(intval($count/10)+1) : intval($count/10);

        $this->v("pageid",$pageid);
        $this->v("page_count",$page_count);
        $this->v('data',$shouquan_data);
        $this->display('daoban');
    }
    //删除授权
    public function del()
    {
        if(IS_POST){
            $id     = intval(X('post.id'));
            if(!S('shouquan')->count(['id'=>$id])){
                return $this->message('删除的内容不存在，可能已被删除。');
            }
            S('shouquan')->delete([
                    "AND" => array(
                        "id" => $id
                    )
                ]);
            return $this->message('删除成功',true);
        }
        return $this->message('请求错误');
    }
    //删除盗版记录
    public function del_daoban()
    {
        if(IS_POST){
            $id     = intval(X('post.id'));
            if(!S('daoban')->count(['id'=>$id])){
                return $this->message('删除的内容不存在，可能已被删除。');
            }
            S('daoban')->delete([
                    "AND" => array(
                        "id" => $id
                    )
                ]);
            return $this->message('删除成功',true);
        }
        return $this->message('请求错误');
    }
    // 系统设置
    public function setup()
    {
        $this->v('title','系统设置');
        if(IS_POST){
            $wwwname = X('post.wwwname');
            $wwwinfo = X('post.wwwinfo');
            $wwwdomain = X('post.wwwdomain');
            $qq      = X('post.qq');
            $no_msg  = X('post.no_msg');
            $key_msg = X('post.key_msg');
            $tongji  = X('post.tongji');
            $debug_page = X('post.debug_page')?1:0;
            $debug   = X('post.debug')?1:0;

            $data   = [
                'wwwname'   => $wwwname,
                'wwwinfo'   => $wwwinfo,
                'wwwdomain' => $wwwdomain,
                'qq'        => $qq,
                'no_msg'    => $no_msg,
                'key_msg'   => $key_msg,
                'tongji'    => $tongji,
                'debug_page'=> $debug_page,
                'debug'     => $debug
            ];

            if(!$debug)
                file_put_contents(INDEX_PATH . 'DEBUG','');
            else{
                if(is_file(INDEX_PATH. 'DEBUG'))
                    unlink(INDEX_PATH. 'DEBUG');
            }
            $data = serialize($data);
            S('config')->update(['config'=>$data],['id'=>1]);
            return $this->message('修改成功',true);
        }

        $this->v('config',$this->conf);
        $this->display('setup'); 
    }
    // 使用文档
    public function work()
    {
        $this->v('title','使用文档');

        $this->display('work'); 
    }

}